//
//  PhotoSet.m
//  PhotoViewer
//
//  Created by Ray Wenderlich on 6/30/10.
//  Copyright 2010 Ray Wenderlich. All rights reserved.
//

#import "PhotoSet.h"
#import "Photo.h"

@implementation PhotoSet
@synthesize title = _title;
@synthesize photos = _photos;

- (id) initWithTitle:(NSString *)title photos:(NSArray *)photos {
    if ((self = [super init])) {
        self.title = title;
        self.photos = photos;
        for(int i = 0; i < _photos.count; ++i) {
            Photo *photo = [_photos objectAtIndex:i];
            photo.photoSource = self;
            photo.index = i;
        }        
    }
    return self;
}

- (void) dealloc {
    self.title = nil;
    self.photos = nil;    
    [super dealloc];
}

#pragma mark TTModel

- (BOOL)isLoading { 
    return FALSE;
}

- (BOOL)isLoaded {
    return TRUE;
}

#pragma mark TTPhotoSource

- (NSInteger)numberOfPhotos {
    return _photos.count;
}

- (NSInteger)maxPhotoIndex {
    return _photos.count-1;
}

- (id<TTPhoto>)photoAtIndex:(NSInteger)photoIndex {
    if (photoIndex < _photos.count) {
        return [_photos objectAtIndex:photoIndex];
    } else {
        return nil;
    }
}

#pragma mark Singleton

static PhotoSet *babyPhotoSet   = nil;
static NSMutableArray *babyPhotos = nil;
static NSMutableArray *photoArray = nil;

+ (PhotoSet *)getBabyPhotoSet:(NSString *) whereClause photoCategory:(int)photoCategory{

    @synchronized(self) {
        if (babyPhotoSet == nil) {
			NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
			Dao *dao = [Dao getInstance];
			if(babyPhotos == nil)
				babyPhotos = [[NSMutableArray alloc]init];
			else 
				[babyPhotos removeAllObjects];
			
			if(photoArray == nil)
				photoArray = [[NSMutableArray alloc]init];
			else 
				[photoArray removeAllObjects];
			
			[dao QueryBabyPhotoEntryWith:whereClause records:babyPhotos];
			
			/* Not autoreleased */
			//NSArray *array = [[NSArray alloc] initWithArray:mutableArray];
			/* Autoreleased array */
			//NSArray *array = [NSArray arrayWithArray:mutableArray];
			for(int i = 0; i < [babyPhotos count]; i++)
			{        
				BabyPhotoEntry *entry = [babyPhotos objectAtIndex:i];
				Photo *pv = [[[Photo alloc] initWithCaption:[entry getSummaryString]
								  urlLarge:[NSString stringWithFormat:@"%@%@",@"documents://",entry.photoPath]
								  urlSmall:[NSString stringWithFormat:@"%@%@",@"documents://",entry.thumPath] 
								  urlThumb:[NSString stringWithFormat:@"%@%@",@"documents://",entry.thumPath] 
								  size:CGSizeMake(320, 320)] autorelease];
								
				[photoArray addObject:pv];
			}
            babyPhotoSet = [[self alloc] initWithTitle:NSLocalizedString(@"Baby Album", @"") photos:photoArray];
			
			[pool release];			
        }
    }
    return babyPhotoSet;
}


+(void)updateBabyPhotoSet:(NSString *) whereClause photoCategory:(int)photoCategory{
    @synchronized(self) {
		
        if (babyPhotoSet != nil) {
			NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

			[babyPhotoSet release];
			Dao *dao = [Dao getInstance];
			if(babyPhotos == nil)
				babyPhotos = [[NSMutableArray alloc]init];
			else 
				[babyPhotos removeAllObjects];
			if(photoArray == nil)
				photoArray = [[NSMutableArray alloc]init];
			else 
				[photoArray removeAllObjects];
			
			[dao QueryBabyPhotoEntryWith:whereClause records:babyPhotos];
			
			for(int i = 0; i < [babyPhotos count]; i++)
			{        
				BabyPhotoEntry *entry = [babyPhotos objectAtIndex:i];
				Photo *pv = [[[Photo alloc] initWithCaption:[entry getSummaryString]
												   urlLarge:[NSString stringWithFormat:@"%@%@",@"documents://",entry.photoPath]
												   urlSmall:[NSString stringWithFormat:@"%@%@",@"documents://",entry.thumPath] 
												   urlThumb:[NSString stringWithFormat:@"%@%@",@"documents://",entry.thumPath] 
													   size:CGSizeMake(320, 320)] autorelease];
				
				[photoArray addObject:pv];
			}
			
            babyPhotoSet = [[self alloc] initWithTitle:NSLocalizedString(@"Baby Album", @"") photos:photoArray];
			
			[pool release];			
        }
    }
}
@end
